package com.gacu.viewer.client.ui.display_area;

import java.util.LinkedList;

import com.gacu.viewer.client.rna.RNAssDrawable;

public class ZoomUtil {

    public static final double zoomQuotient = 0.8;
    
    //TODO : faire un type abstrait ou une interface a place
    private static LinkedList<Scaler> scalers;
    
    public enum zoomAction
    {
		zoomIn,
		zoomOut
    }

    private ZoomUtil()
    {
    	
    }

    public static void doZoom(Drawer dr, zoomAction action)
    {
		if (action == zoomAction.zoomIn)
		{
		    doZoomIn(dr, zoomQuotient);
		}
		else if (action == zoomAction.zoomOut)
		{
		    doZoomOut(dr, zoomQuotient);
		}
    }

    public static void setZoom(Drawer dr, double quotient){
		double newQuotient;
		
		newQuotient = quotient / dr.scaleFactor;
		
		if(quotient > dr.scaleFactor)
		{
		    doZoomOut(dr,newQuotient);		   
		}
		else if (quotient < dr.scaleFactor)
		{
		    doZoomIn(dr, 1.0 / newQuotient);
		}
    }

    private static void doZoomIn(Drawer dr, double quotient) {
    	double newQuotient;
    	
    	newQuotient = 1.0/quotient;
    	
    	dr.scaleFactor = dr.scaleFactor * newQuotient;
		for(RNAssDrawable po : dr.pool.getAll())
		{
		    po.scale(newQuotient);
		}
		
		updateScalers(dr.scaleFactor);
    }

    private static void doZoomOut(Drawer dr, double quotient){
    	double newQuotient;
    	
    	newQuotient = quotient;
    	
    	dr.scaleFactor = dr.scaleFactor * newQuotient;
    	for(RNAssDrawable po : dr.pool.getAll())
		{
		    po.scale(newQuotient);
		}
    	
    	updateScalers(dr.scaleFactor);
    }
    
    public static void addScaler(Scaler sc)
    {
    	if (scalers == null)
    	{
    		scalers = new LinkedList<Scaler>();
    	}
    	scalers.add(sc);
    }
    
    private static void updateScalers(double scale)
    {
    	if (scalers != null)
    	{
    		for(Scaler sc : scalers)
    		{
    			sc.trySetScale(scale);
    		}
    	}
    }
}
